﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Host.Models
{
    public class RuleModel
    {
        public int PageSize { get; set; }

        public int PageNumber { get; set; }

        public IEnumerable<MapQuestionAndAnswer> Questions { get; set; }

        public int TotalRecords { get; set; }

        public int FundId { get; set; }

        public int QuestionGroup { get; set; }

        public IEnumerable<SelectListItem> QuestionGroups
        { 
            get
            {
                return new SelectList(new Data().QuestionGroup.Select(x => x), "QuestionGroupId", "Description"); 
            }
        }

        public IEnumerable<SelectListItem> Funds
        { 
            get { return new SelectList(new Data().Fund.Select(x => x), "FundId", "FundName"); }
        }

        public IEnumerable<SelectListItem> AnswerTypes{
            get
            {
                var q = new Data().AnswerDataType.Select(x => x);
                List<AnswerDataType> list = new List<AnswerDataType>();
                foreach (var answerDataType in q)
                {
                     switch (answerDataType.AnswerDataTypeDescription)
                {
                    case "Boolean":
                             list.Add(new AnswerDataType{ AnswerDataTypeId = answerDataType.AnswerDataTypeId, AnswerDataTypeDescription ="True / False" });
                        break;
                    case "String":
                       list.Add(new AnswerDataType{ AnswerDataTypeId = answerDataType.AnswerDataTypeId, AnswerDataTypeDescription ="Text" });
                        break;
                    case "Integer":
                        list.Add(new AnswerDataType{ AnswerDataTypeId = answerDataType.AnswerDataTypeId, AnswerDataTypeDescription ="Number" });
                        break;
                    case "Numeric":
                     list.Add(new AnswerDataType{ AnswerDataTypeId = answerDataType.AnswerDataTypeId, AnswerDataTypeDescription ="Decimal Number" });  
                        break;
                    case "Count":
                      list.Add(new AnswerDataType{ AnswerDataTypeId = answerDataType.AnswerDataTypeId, AnswerDataTypeDescription ="Count" });  
                        break;
                    case "Compare":
                       list.Add(new AnswerDataType{ AnswerDataTypeId = answerDataType.AnswerDataTypeId, AnswerDataTypeDescription ="Compare" }); 
                        break;
                }

                }

                return new SelectList(list, "AnswerDataTypeId", "AnswerDataTypeDescription");
            }
        }

        public IEnumerable<SelectListItem> QuestionCategorys{
            get
            {
                return new SelectList(new Data().QuestionCategory.Select(x => x), "QuestionCategoryId", "Category"); 
            }
        }

        public RuleModel GetData(int startPage, int pageSize, int questionGroupId, int fundId)
        {
            //.Skip(page * totalRecords).Take(pageSize)
            List<MapQuestionAndAnswer> newList = new List<MapQuestionAndAnswer>();
            using (Data db = new Data())
            {
                var list = db.FundAnswerSheet.Where(i => i.FundId == fundId).Select(x => new MapQuestionAndAnswer
                {
                    FundAnswerSheetId = x.FundAnswerSheetId,
                    Answer = x.AnswerValue,
                    QuestionId = x.QuestionId,
                    FundId = fundId,
                    QuestionGroupId = questionGroupId
                });


                foreach (var mapQuestionAndAnswer in list)
                {

                    var q = db.Question.Where(i => i.QuestionId == mapQuestionAndAnswer.QuestionId).Select(x => x).Single();
                    if (q.QuestionGroupId == questionGroupId)
                    {
                        mapQuestionAndAnswer.Description = q.Question1;
                        AnswerDataType a =
                            db.AnswerDataType.Where(i => i.AnswerDataTypeId == q.AnswerDataTypeId).Select(x => x).Single();
                        mapQuestionAndAnswer.AnswerType = a.AnswerDataTypeDescription;
                        newList.Add(mapQuestionAndAnswer);
                    }
                }
            }
            int totalRecord = newList.Count();
            var filtered = newList.Skip(startPage * pageSize).Take(pageSize).AsEnumerable();
            RuleModel Model = new RuleModel() { PageSize = pageSize, PageNumber = startPage, Questions = filtered, TotalRecords = totalRecord };

            return Model;

        }

    }


    public class MapQuestionAndAnswer
    {
        public int FundAnswerSheetId { get; set; }
        public string Description { get; set; }
        public string Answer { get; set; }
        public string AnswerType { get; set; }
        public int QuestionId { get; set; }
        public int FundId { get; set; }
        public int QuestionGroupId { get; set; }

    }

    public class QuestionModel
    {
        public int PageSize { get; set; }

        public int PageNumber { get; set; }

        public IEnumerable<MapQuestionAndAnswer> Questions { get; set; }

        public int TotalRecords { get; set; }
    }
}